module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('departments', {
      id: {
        type: Sequelize.UUID,
        defaultValue: Sequelize.UUIDV4,
        primaryKey: true,
      },
      tenant_id: {
        type: Sequelize.UUID,
        allowNull: false,
        references: {
          model: 'tenants',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      company_id: {
        type: Sequelize.UUID,
        allowNull: false,
        references: {
          model: 'companies',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      parent_id: {
        type: Sequelize.UUID,
        allowNull: true,
        references: {
          model: 'departments',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: '上级部门ID',
      },
      name: {
        type: Sequelize.STRING(255),
        allowNull: false,
        comment: '部门名称',
      },
      code: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: '部门代码',
      },
      manager_id: {
        type: Sequelize.UUID,
        allowNull: true,
        references: {
          model: 'users',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'SET NULL',
        comment: '部门负责人ID',
      },
      phone: {
        type: Sequelize.STRING(50),
        allowNull: true,
        comment: '部门电话',
      },
      email: {
        type: Sequelize.STRING(255),
        allowNull: true,
        comment: '部门邮箱',
      },
      description: {
        type: Sequelize.TEXT,
        allowNull: true,
        comment: '部门描述',
      },
      level: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 1,
        comment: '部门层级（1为顶级）',
      },
      path: {
        type: Sequelize.STRING(500),
        allowNull: true,
        comment: '部门路径（如：/dept1/dept2/dept3）',
      },
      status: {
        type: Sequelize.ENUM('active', 'inactive'),
        allowNull: false,
        defaultValue: 'active',
        comment: '状态',
      },
      sort_order: {
        type: Sequelize.INTEGER,
        allowNull: false,
        defaultValue: 0,
        comment: '排序序号',
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
      },
      deleted_at: {
        type: Sequelize.DATE,
        allowNull: true,
      },
    });

    // 添加索引
    await queryInterface.addIndex('departments', ['tenant_id']);
    await queryInterface.addIndex('departments', ['company_id']);
    await queryInterface.addIndex('departments', ['parent_id']);
    await queryInterface.addIndex('departments', ['manager_id']);
    await queryInterface.addIndex('departments', ['status']);
    await queryInterface.addIndex('departments', ['level']);
    
    // 公司+部门代码唯一
    await queryInterface.addIndex('departments', ['company_id', 'code'], {
      unique: true,
      name: 'departments_company_id_code_unique',
      where: {
        code: { [Sequelize.Op.ne]: null },
        deleted_at: null,
      },
    });
  },

  down: async (queryInterface) => {
    await queryInterface.dropTable('departments');
  },
};

